using UnityEngine;

namespace MyFramework.Singleton
{
    /// <summary>
    /// 继承自MonoBehaviour的单例模式类模板
    /// <br></br>
    /// 继承后自动创建对象，不需要手动添加
    /// </summary>
    /// <typeparam name="T">需要的类型</typeparam>
    public abstract class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T _instance;

        public static T GetInstance()
        {
            if (_instance) return _instance;
            var tempObj = new GameObject
            {
                name = typeof(T).ToString()
            };
            DontDestroyOnLoad(tempObj);
            _instance = tempObj.AddComponent<T>();

            return _instance;
        }
    }
}